<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let model_id = this.$get('model_id');
    let method_map = {
      'eq':'等于(=)', 'neq':'不等于(<>)',
      'gt':'大于(>)', 'egt':'大于或等于(>=)',
      'lt':'小于(<)', 'elt':'小于或等于(<=)',
      'like':'包含(like)', 'notlike':'不包含(not like)',
      'in':'在列表中(in)', 'notin':'不在列表中(not in)',
      'between':'介于之间(between)', 'notbetween':'不介于之间(not between)',
    }
    let table_map = this.admin_config('status_alias_map.model_field_where.table');
    let type_map = this.field_type(this.field_type.CAN_MODEL_FIELD_WHERE, 'label');
    let forms_config = {
      model_id: {label:'模型',name:'model_id',type:'select_one',config:[],tips:'', value:model_id||''},
      table: {label:'所属表',name:'table',type:'select_one',config:table_map,value:0},
      name: {label:'字段名',name:'name',type:'text_one',tips:'字段名，关联字段用{table}.{field}，例如category.name'},
      method: {label:'检索方式',name:'method',type:'select_one',tips:'',config:method_map,value:'eq'},
      label: {label:'别名',name:'label',type:'text_one',tips:''},
      tips: {label:'提示',name:'tips',type:'text_one',tips:'字段注释、提示'},
      type: {label:'输入类型',name:'type',type:'select_one',config:type_map,tips:'选择输入的组件类型',value:''},
      config: {label:'组件配置',name:'config',type:'text_more',tips:''},
      value: {label:'组件默认值',name:'value',type:'text_one',tips:'默认值'},
      sort: {label:'排序',name:'sort',type:'text_one',tips:'排序索引，大>小', value:0},
    }
    if (id){
      forms_config.model_id.is_info = true;
      forms_config.table.is_info = true;
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.id ? `/admin/model_field_where/edit?id=`+this.id : '/admin/model_field_where/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('index', '$refs.list_url.list');
          iframe.exe('/model/all?id='+this.model_id, 'model_field_where_list');
          iframe.hide('edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    }
  },
  watch: {
    'config.type.value'(newValue){
      s94Mjs.each(field_type.component_config(newValue), (v,k)=>{
        this.config.config[k] = v;
      })
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/model_field_where/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit');
        }else {
          this.$router.push('index');
        }
      })
    }else{
      app.config.type.value = 'text_one';
    }

    //其他初始化操作
    ajax({url:'/admin/model/list',cache:true},function(res){
      app.config.model_id.config = app.decode_config(res.data.list, {content_key:'alias', value_key:'id'});
      return false
    })
  }
}
</script>
